﻿using heyirunde.Models;
using ImageProcessor;
using ImageProcessor.Imaging.Formats;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;

namespace heyirunde.variable
{
    public class SaveImg
    {
        private static string imagePathPublic = @"C:/manager/image/";
        private static string imagePathweb = @"/image/";  

        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <param name="sFullName"></param>
        /// <returns></returns>
        public static long GetFileSize(string sFullName)
        {
            long lSize = 0;
            if (File.Exists(sFullName))
                lSize = new FileInfo(sFullName).Length;
            return lSize;
        }

        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string CountSize(long Size)
        {
            string m_strSize = "";
            long FactSize = 0;
            FactSize = Size;
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }

        /// <summary>
        /// 保存图片，图片管理器
        /// </summary>
        /// <param name="files"></param>
        /// <param name="type"></param>
        /// <param name="typeId"></param>
        /// <param name="userGuid"></param>
        public static void SaveMinImg(HttpPostedFile file, int type, string userGuid)
        {
            using (var db = new SqlSugarClient(DBHelper.connectionString))
            {
                string filename = DateTime.Now.ToString("yyyyMMdd") + "/";
                //如果对应的文件夹不存在，则新建一个文件夹
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(filename)))        //如果该方法是静态方法则 Server 前要添加一些东西
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filename));
                }
                // 检测格式
                ISupportedImageFormat format = new PngFormat { Quality = 70 };
                Size size = new Size(180, 150);
                Image image;
                string path;
                Image imagemin;
                string pathmin;
                int width;
                int height;
                string imgsize;
                string imgname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                string imgnamemin = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "min.png";
                string fileurl = filename + imgname;
                string fileurlmin = filename + imgnamemin;
                using (MemoryStream outStream = new MemoryStream())
                {
                    // 使用重载初始化ImageFactory以保留EXIF元数据。
                    using (ImageFactory imageFactory = new ImageFactory(true))
                    {
                        // 加载，调整大小，设置格式和质量并保存图像。
                        imageFactory.Load(file.InputStream)
                            .Format(format)
                            .Save(imagePathPublic + fileurl);
                        //对获取的imageFactory对象进行对应的操作        
                        image = imageFactory.Image;
                        width = imageFactory.Image.Width;
                        height = imageFactory.Image.Height;
                        imgsize = CountSize(GetFileSize((imagePathPublic + fileurl)));
                        path = imagePathweb + fileurl;
                    }
                    using (ImageFactory imageFactory1 = new ImageFactory(true))
                    {
                        // 加载，调整大小，设置格式和质量并保存图像。
                        imageFactory1.Load(file.InputStream)
                                    .Saturation(25)//饱和度
                                    .Resize(size)//大小
                                    .Format(format)
                                    .Save(imagePathPublic + fileurlmin)
                                    ;
                        //对获取的imageFactory对象进行对应的操作        
                        imagemin = imageFactory1.Image;
                        pathmin = imagePathweb + fileurlmin;
                    }
                    ImgManager img = new ImgManager()
                    {
                        userGuid = userGuid,
                        imgurl = path,
                        miniurl = pathmin,
                        width = width,
                        height = height,
                        size = imgsize,
                        type = type,
                        createTime = DateTime.Now,
                        isdel = 0
                    };
                    db.Insert(img);
                }
            }
        }
        //保存商家上传图片(营业执照)
        public static void SaveSJPicture(HttpFileCollection files, int type, string userGuid,int typeId)
        {
            using (var db = new SqlSugarClient(DBHelper.connectionString))
            {
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    string filename = DateTime.Now.ToString("yyyyMMdd") + "/";
                    //如果对应的文件夹不存在，则新建一个文件夹
                    if (!Directory.Exists(HttpContext.Current.Server.MapPath(filename)))        //如果该方法是静态方法则 Server 前要添加一些东西
                    {
                        Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filename));
                    }
                    // 检测格式
                    ISupportedImageFormat format = new PngFormat { Quality = 70 };
                    Image image;
                    string path;
                    int width;
                    int height;
                    string imgsize;
                    string imgname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                    string imgnamemin = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "min.png";
                    string fileurl = filename + imgname;
                    string fileurlmin = filename + imgnamemin;
                    using (MemoryStream outStream = new MemoryStream())
                    {
                        // 使用重载初始化ImageFactory以保留EXIF元数据。
                        using (ImageFactory imageFactory = new ImageFactory(true))
                        {
                            // 加载，调整大小，设置格式和质量并保存图像。
                            imageFactory.Load(file.InputStream)
                                .Format(format)
                                .Save(imagePathPublic + fileurl);
                            //对获取的imageFactory对象进行对应的操作        
                            image = imageFactory.Image;
                            width = imageFactory.Image.Width;
                            height = imageFactory.Image.Height;
                            imgsize = CountSize(GetFileSize((imagePathPublic + fileurl)));
                            path = imagePathweb + fileurl;
                        }
                        ImgManager img = new ImgManager()
                        {
                            imgurl = path,
                            width = width,
                            height = height,
                            size = imgsize,
                            type = type,
                            typeId= typeId,
                            userGuid = userGuid,
                            createTime = DateTime.Now,
                            isdel = 0
                        };
                        db.Insert(img);
                    }
                }
            }
        }
        //保存商家上传图片(Logo)
        public static void SaveSJLogo(HttpPostedFile file, int type, string userGuid, int typeId)
        {
            using (var db = new SqlSugarClient(DBHelper.connectionString))
            {             
                    string filename = DateTime.Now.ToString("yyyyMMdd") + "/";
                    //如果对应的文件夹不存在，则新建一个文件夹
                    if (!Directory.Exists(HttpContext.Current.Server.MapPath(filename)))        //如果该方法是静态方法则 Server 前要添加一些东西
                    {
                        Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filename));
                    }
                    // 检测格式
                    ISupportedImageFormat format = new PngFormat { Quality = 70 };
                    Image image;
                    string path;
                    int width;
                    int height;
                    string imgsize;
                    string imgname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                    string imgnamemin = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "min.png";
                    string fileurl = filename + imgname;
                    string fileurlmin = filename + imgnamemin;
                using (MemoryStream outStream = new MemoryStream())
                {
                    // 使用重载初始化ImageFactory以保留EXIF元数据。
                    using (ImageFactory imageFactory = new ImageFactory(true))
                    {
                        // 加载，调整大小，设置格式和质量并保存图像。
                        imageFactory.Load(file.InputStream)
                            .Format(format)
                            .Save(imagePathPublic + fileurl);
                        //对获取的imageFactory对象进行对应的操作        
                        image = imageFactory.Image;
                        width = imageFactory.Image.Width;
                        height = imageFactory.Image.Height;
                        imgsize = CountSize(GetFileSize((imagePathPublic + fileurl)));
                        path = imagePathweb + fileurl;
                    }
                    ImgManager img = new ImgManager()
                    {
                        imgurl = path,
                        width = width,
                        height = height,
                        size = imgsize,
                        type = type,
                        typeId = typeId,
                        userGuid = userGuid,
                        createTime = DateTime.Now,
                        isdel = 0
                    };
                    db.Insert(img);
                }
                
            }
        }
        //保存商家上传图片(商品图片)
        public static string SaveGoodsPic(HttpFileCollection files, int type, string userGuid, int typeId)
        {
            using (var db = new SqlSugarClient(DBHelper.connectionString))
            {
                int insert = 0;
                string ids = "";
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    string filename = DateTime.Now.ToString("yyyyMMdd") + "/";
                    //如果对应的文件夹不存在，则新建一个文件夹
                    if (!Directory.Exists(HttpContext.Current.Server.MapPath(filename)))        //如果该方法是静态方法则 Server 前要添加一些东西
                    {
                        Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filename));
                    }
                    // 检测格式
                    ISupportedImageFormat format = new PngFormat { Quality = 70 };
                    Image image;
                    string path;
                    int width;
                    int height;
                    string imgsize;
                    string imgname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                    string imgnamemin = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "min.png";
                    string fileurl = filename + imgname;
                    string fileurlmin = filename + imgnamemin;
                    using (MemoryStream outStream = new MemoryStream())
                    {
                        // 使用重载初始化ImageFactory以保留EXIF元数据。
                        using (ImageFactory imageFactory = new ImageFactory(true))
                        {
                            // 加载，调整大小，设置格式和质量并保存图像。
                            imageFactory.Load(file.InputStream)
                                .Format(format)
                                .Save(imagePathPublic + fileurl);
                            //对获取的imageFactory对象进行对应的操作        
                            image = imageFactory.Image;
                            width = imageFactory.Image.Width;
                            height = imageFactory.Image.Height;
                            imgsize = CountSize(GetFileSize((imagePathPublic + fileurl)));
                            path = imagePathweb + fileurl;
                        }
                        ImgManager img = new ImgManager()
                        {
                            imgurl = path,
                            width = width,
                            height = height,
                            size = imgsize,
                            type = type,
                            typeId = typeId,
                            userGuid = userGuid,
                            createTime = DateTime.Now,
                            isdel = 0
                        };
                        insert = db.Insert(img).ObjToInt();                        
                    }
                    ids += (insert + ";").ToString();
                }
                return ids;
            }
            
        }











    }
}